package com.ruoyi.tsjData.service.impl;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.calculate.BezierCurveUtil;
import com.ruoyi.common.utils.jkalman.JKalmanUtils;
import com.ruoyi.tsjData.domain.plat.DataTshjSd;
import com.ruoyi.tsjData.domain.plat.TshjSdParaChange;
import com.ruoyi.tsjData.service.IDataTshjChangeService;
import com.ruoyi.tsjData.service.IDataTshjSdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 提升机任务变更测试项参数Service业务层处理
 */
@Service
public class DataTshjChangeServiceImpl implements IDataTshjChangeService {

    protected Logger log = LoggerFactory.getLogger(DataTshjChangeServiceImpl.class);

    @Autowired
    private IDataTshjSdService dataTshjSdService;

    /**
     * 最大速度比例变更
     * @param tshjSdParaChange 需要修改的最大速度
     * @return
     */
    @Override
    public DataTshjSd modifySdMax(TshjSdParaChange tshjSdParaChange) {
        DataTshjSd dataTshjSd = dataTshjSdService.selectDataTshjSdBySdId(tshjSdParaChange.getSdId());
        //原最大速度
        log.info("原始最大速度：{} ", dataTshjSd.getSdMax());
        //变更的最大速度
        log.info("变更最大速度：{} ", tshjSdParaChange.getSdMax());
        if (!StringUtils.isEmpty(dataTshjSd.getSuduList()) && !StringUtils.isEmpty(dataTshjSd.getSdMax()) && !StringUtils.isEmpty(tshjSdParaChange.getSdMax())) {
            float sdMaxF = Float.parseFloat(dataTshjSd.getSdMax());
            float changeSdMaxF = Float.parseFloat(tshjSdParaChange.getSdMax());
            float radio = changeSdMaxF / sdMaxF;
            List<Float> suduVelList = JSON.parseArray(dataTshjSd.getSuduList(), Float.class);
            List<Float> suduVelChangeList = new ArrayList<>();
            suduVelList.forEach(item -> {
                suduVelChangeList.add(item * radio);
            });
            dataTshjSd.setSuduList(JSON.toJSONString(suduVelChangeList));
            //生成速度拟合
            JKalmanUtils.getInstance().release();
            List<Float> suduNiheVelList = JSON.parseArray(dataTshjSd.getSuduList(), Float.class);
            List<Float> sdNiheList = new ArrayList<>();
            sdNiheList.addAll(JKalmanUtils.getInstance().correctList(suduNiheVelList));
            // 电流曲线数据
            dataTshjSd.setDlChartLineDTO(BezierCurveUtil.calPoints(dataTshjSd.getDlList(), 0.2f, true));
            // 速度曲线数据
            dataTshjSd.setSdChartLineDTO(BezierCurveUtil.calPoints(sdNiheList, 0.1f, true));
            // 速度拟合曲线数据
            dataTshjSd.setSdNiheChartLineDTO(BezierCurveUtil.calPoints(sdNiheList, 0.1f, false));
            List<Float> q40List = sdNiheList.subList(0, sdNiheList.size() < 400 ? sdNiheList.size() - 1 : 400);
            List<Float> h40List = sdNiheList.subList(sdNiheList.size() > 400 ? sdNiheList.size() - 400 : 0, sdNiheList.size() - 1);
            // 前40s速度曲线数据
            dataTshjSd.setSdQ40ChartLineDTO(BezierCurveUtil.calPoints(q40List, 0.1f, true));
            // 后40s速度曲线数据
            dataTshjSd.setSdH40ChartLineDTO(BezierCurveUtil.calPoints(h40List, 0.1f, true));
            // 加速度集合
            List<Float> jsdVelList = new ArrayList<>();
            for (int i = 1; i < suduVelList.size(); i++) {
                jsdVelList.add((sdNiheList.get(i) - sdNiheList.get(i - 1)) / 0.1f);
            }
            // 加速度曲线数据
            dataTshjSd.setJsdChartLineDTO(BezierCurveUtil.calPoints(jsdVelList, 0.1f, true));
            // 井深速度图
            List<Float> jsList = new ArrayList<>();
            float sum = 0f;
            for (Float sdVel : suduVelList) {
                sum += (sdVel * 0.1f);
                jsList.add(sum);
            }
            float sum_last = jsList.get(jsList.size() - 1);
            //系数
            float sumRatio = sum_last == 0 ? 0f : Float.parseFloat(tshjSdParaChange.getParaTshjl()) / sum_last;
            List<Float> jsDdList = new ArrayList<>();
            List<Float> suduList = new ArrayList<>();
            for (int k = 0; k < jsList.size(); k++) {
                float key = jsList.get(k) * sumRatio;
                if(!jsDdList.contains(key)) {
                    jsDdList.add(key);
                    suduList.add(suduVelList.get(k));
                }
            }
            dataTshjSd.setSdjshChartLineDTO(BezierCurveUtil.calPoints(jsDdList, suduList, true));
        }
        //变更井深
        dataTshjSd.setParaTshjl(tshjSdParaChange.getParaTshjl());
        //最大速度变更
        dataTshjSd.setSdMax(String.format("%.2f", dataTshjSd.getSdjshChartLineDTO().getMaxYLinePoint().y));
        return dataTshjSd;
    }

    /**
     * 提升距离调整变更
     * @param tshjSdParaChange 需要修改的提升距离
     * @return
     */
    @Override
    public DataTshjSd modifyTshjl(TshjSdParaChange tshjSdParaChange) {
        DataTshjSd dataTshjSd = dataTshjSdService.selectDataTshjSdBySdId(tshjSdParaChange.getSdId());
        //原提升距离
        log.info("原始提升距离：{} ", dataTshjSd.getParaTshjl());
        //变更的提升距离
        log.info("变更提升距离：{} ", tshjSdParaChange.getParaTshjl());
        if (dataTshjSd.getCsfsh().equals("0") && !StringUtils.isEmpty(tshjSdParaChange.getParaTshjl())) {
            float radio = Float.parseFloat(tshjSdParaChange.getParaTshjl()) / Float.parseFloat(dataTshjSd.getParaTshjl());
            //原始速度点位
            List<Float> suduVelList = JSON.parseArray(dataTshjSd.getSuduList(), Float.class);
            //速度新点位
            List<Float> y_speedf = new ArrayList<>();
            //速度拟合点位
            List<Float> sdNiheList = new ArrayList<>();
            float speed = 0;
            for (Float aFloat : suduVelList) {
                if (dataTshjSd.getCsfsh().equals("0")) {
                    speed = radio * aFloat;
                }
                y_speedf.add(speed);
            }
            JKalmanUtils.getInstance().release();
            sdNiheList.addAll(JKalmanUtils.getInstance().correctList(y_speedf));
            for (Float aFloat : sdNiheList) {
                if (aFloat > Float.parseFloat(dataTshjSd.getSdMax())) {
                    dataTshjSd.setSdMax(String.valueOf(aFloat));
                }
            }
            // 电流曲线数据
            dataTshjSd.setDlChartLineDTO(BezierCurveUtil.calPoints(dataTshjSd.getDlList(), 0.2f, true));
            // 速度曲线数据
            dataTshjSd.setSdChartLineDTO(BezierCurveUtil.calPoints(sdNiheList, 0.1f, true));
            // 速度拟合曲线数据
            dataTshjSd.setSdNiheChartLineDTO(BezierCurveUtil.calPoints(sdNiheList, 0.1f, false));
            List<Float> q40List = sdNiheList.subList(0, sdNiheList.size() < 400 ? sdNiheList.size() - 1 : 400);
            List<Float> h40List = sdNiheList.subList(sdNiheList.size() > 400 ? sdNiheList.size() - 400 : 0, sdNiheList.size() - 1);
            // 前40s速度曲线数据
            dataTshjSd.setSdQ40ChartLineDTO(BezierCurveUtil.calPoints(q40List, 0.1f, true));
            // 后40s速度曲线数据
            dataTshjSd.setSdH40ChartLineDTO(BezierCurveUtil.calPoints(h40List, 0.1f, true));
            // 加速度集合
            List<Float> jsdVelList = new ArrayList<>();
            for (int i = 1; i < sdNiheList.size(); i++) {
                jsdVelList.add((sdNiheList.get(i) - sdNiheList.get(i - 1)) / 0.1f);
            }
            // 加速度曲线数据
            dataTshjSd.setJsdChartLineDTO(BezierCurveUtil.calPoints(jsdVelList, 0.1f, true));
            // 井深速度图
            List<Float> jsList = new ArrayList<>();
            float sum = 0f;
            for (Float sdVel : suduVelList) {
                sum += (sdVel * 0.1f);
                jsList.add(sum);
            }
            float sum_last = jsList.get(jsList.size() - 1);
            //系数
            float sumRatio = sum_last == 0 ? 0f : Float.parseFloat(tshjSdParaChange.getParaTshjl()) / sum_last;
            List<Float> jsDdList = new ArrayList<>();
            List<Float> suduList = new ArrayList<>();
            for (int k = 0; k < jsList.size(); k++) {
                float key = jsList.get(k) * sumRatio;
                if(!jsDdList.contains(key)) {
                    jsDdList.add(key);
                    suduList.add(suduVelList.get(k));
                }
            }
            dataTshjSd.setSdjshChartLineDTO(BezierCurveUtil.calPoints(jsDdList, suduList, true));
        }
        //变更井深
        dataTshjSd.setParaTshjl(tshjSdParaChange.getParaTshjl());
        //最大速度变更
        dataTshjSd.setSdMax(String.format("%.2f", dataTshjSd.getSdjshChartLineDTO().getMaxYLinePoint().y));
        return dataTshjSd;
    }

    /**
     * 变更参数后生成新曲线图
     * @param dataTshjSd
     * @return
     */
    @Override
    @Async("threadPoolKy")
    public Future<DataTshjSd> cal(DataTshjSd dataTshjSd) {
        if (dataTshjSd.getSuduList() != null) {
            // 电流曲线数据
            dataTshjSd.setDlChartLineDTO(BezierCurveUtil.calPoints(dataTshjSd.getDlList(), 0.2f, true));
            // 速度曲线数据
            dataTshjSd.setSdChartLineDTO(BezierCurveUtil.calPoints(dataTshjSd.getSuduList(), 0.1f, true));
            List<Float> suduVelList = JSON.parseArray(dataTshjSd.getSuduList(), Float.class);
            List<Float> q40List = suduVelList.subList(0, suduVelList.size() < 400 ? suduVelList.size() - 1 : 400);
            List<Float> h40List = suduVelList.subList(suduVelList.size() > 400 ? suduVelList.size() - 400 : 0, suduVelList.size() - 1);
            // 前40s速度曲线数据
            dataTshjSd.setSdQ40ChartLineDTO(BezierCurveUtil.calPoints(q40List, 0.1f, true));
            // 后40s速度曲线数据
            dataTshjSd.setSdH40ChartLineDTO(BezierCurveUtil.calPoints(h40List, 0.1f, true));
            // 加速度集合
            List<Float> jsdVelList = new ArrayList<>();
            for (int i = 1; i < suduVelList.size(); i++) {
                jsdVelList.add((suduVelList.get(i) - suduVelList.get(i - 1)) / 0.1f);
            }
            // 加速度曲线数据
            dataTshjSd.setJsdChartLineDTO(BezierCurveUtil.calPoints(jsdVelList, 0.1f, true));
            // 井深速度图
            List<Float> jsList = new ArrayList<>();
            float sum = 0f;
            for (Float sdVel : suduVelList) {
                sum += (sdVel * 0.1f);
                jsList.add(sum);
            }
            float sum_last = jsList.get(jsList.size() - 1);
            //系数
            float sumRatio = sum_last == 0 ? 0f : Float.parseFloat(dataTshjSd.getParaTshjl()) / sum_last;
            List<Float> jsDdList = new ArrayList<>();
            for (Float aFloat : jsList) {
                float key = aFloat * sumRatio;
                jsDdList.add(key);
            }
            dataTshjSd.setSdjshChartLineDTO(BezierCurveUtil.calPoints(jsDdList, suduVelList, true));
        }
        return new AsyncResult<>(dataTshjSd);

    }
}
